package com.boco.fengke.logintest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class MainActivity extends AppCompatActivity {

    private Button loginButton;
    private TextView resultTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        resultTextView = (TextView)findViewById(R.id.resultTextView);
        loginButton = (Button) findViewById(R.id.loginButton);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send();
            }
        });
    }

    private void send(){
//            params.setSslSocketFactory(...); // 设置ssl
//            params.addQueryStringParameter("wd", "xUtils");
//            JSONObject json = getJSON();
//            if(json != null){
//                params.addParameter("register",json);
//            }
        RequestParams params = new RequestParams("http://172.25.50.1:8080/ssm/api/home/register");
        //account=15100000000&password=654321&device_id=F1.11&user_type=0&captcha=686771&timestamp=1111111111111
        params.addParameter("account","15101111111");
        params.addParameter("password","1231");
        params.addParameter("device_id","1231242");
        params.addParameter("user_type",0);
        params.addParameter("captcha",1234);
        params.addParameter("timestamp",11111111);
        android.util.Log.d("fengke000","params = " + params.toString());
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
//                    Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();
                    resultTextView.setText(result);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
//                    Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
                    android.util.Log.d("fengke000","wrong " + ex.toString());
                }

                @Override
                public void onCancelled(CancelledException cex) {
                    Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onFinished() {

                }
            });
    }

//http://localhost:8080/ssm/api/home/register?account=15100000000&password=654321&device_id=F1.11&user_type=0&captcha=686771&timestamp=1111111111111
    private JSONObject getJSON(){
        JSONObject jsonObject =  new JSONObject();
        try{
            jsonObject.put("account","10086");
            jsonObject.put("password","12345678");
            jsonObject.put("device_id","888888888888888888888");
            jsonObject.put("user_type",0);
            jsonObject.put("captcha",1234);
            jsonObject.put("timestamp","2016-05-26 16:37:12:0000");
        } catch (JSONException e){
            return null;
        }
        return jsonObject;
    }
}
